from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_core.messages import HumanMessage
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_ollama import OllamaEmbeddings
from langchain.tools.retriever import create_retriever_tool
import os
from langchain_openai import ChatOpenAI
from langchain_tavily import TavilySearch

# prepare the embedding model
embeddings_model = OllamaEmbeddings(
        base_url='http://192.168.2.208:11434',
        model="nomic-embed-text")

loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(
    chunk_size=1000, chunk_overlap=200
).split_documents(docs)
vector = FAISS.from_documents(documents, embeddings_model)
retriever = vector.as_retriever()

retriever_tool = create_retriever_tool(
    retriever,
    "langsmith_search",
    "Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
)

os.environ["TAVILY_API_KEY"] = "XXXXXX"
tavily_tool = TavilySearch(
    max_results=5
)
tools = [tavily_tool, retriever_tool]

# DeepSeek API
llm = ChatOpenAI(
    api_key = 'sk-XXXX',
    base_url = 'https://api.deepseek.com/v1',
    model='deepseek-chat'# 或其他 DeepSeek 模型
)
model_with_tools = llm.bind_tools(tools)
response = model_with_tools.invoke([HumanMessage(content="What's the weather in SF?")])

print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")